package com.business.log;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.StringTokenizer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.beans.log.LogInfo;
import com.sql.DBFactory;

public class LogInfoBus {
	private static final Log log = LogFactory.getLog(LogInfoBus.class);

	private Connection conn = null;

	private Statement stmt = null;

	private PreparedStatement pstmt = null;



	private String sql = null;

	public void addLog(LogInfo li) {
		String logCategory = li.getLogCategory();
		String isQuote = li.getIsQuote();
		String isShow = li.getIsShow();
		String logAuthor = li.getLogAuthor();
		String logContent = li.getLogContent();
		String logIntro = li.getLogIntro();
		String logNo = li.getLogNo();
		String logPostTime = li.getLogPostTime();
		String logQuoteId = li.getLogQuoteId();
		String logQuotenums = li.getLogQuotenums();
		String logTitle = li.getLogTitle();
		String logtypeId = li.getLogtypeId();
		String replynum = li.getReplynum();
		String viewnums = li.getViewnums();
		String logTag = li.getLogTag();
		String sql = "insert into log_info(logtype_id,log_title,log_intro,log_author";
		sql += ",isQuote,log_content,log_postTime,viewnums,log_quotenums,isShow";
		sql += ",replynum,log_quote_id,log_no,log_category,log_tag,log_modifytime)";
		sql += " values(" + logtypeId + ",'" + logTitle + "','" + logIntro
				+ "','" + logAuthor + "',";
		sql += isQuote + ",'" + logContent + "','" + logPostTime + "',"
				+ viewnums + "," + logQuotenums + "," + isShow + ",";
		sql += replynum + "," + logQuoteId + ",'" + logNo + "','" + logCategory
				+ "','" + logTag + "',now())";

		try {
			conn = DBFactory.getConnection();
			conn.setAutoCommit(false);
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
		} catch (Exception e) {
			log.error("the insert loginfo exception:" + e.getMessage());
			log.error("the insert loginfo sql is: " + sql);
		}
		String sqlTag = "insert into log_tag(tag_name,view_count)values(?,'0')";
		try {
			pstmt = conn.prepareStatement(sqlTag);
			StringTokenizer s = new StringTokenizer(logTag, ",");
			while (s.hasMoreTokens()) {
				String tagName = s.nextToken();
				pstmt.setString(1, tagName);
				pstmt.execute();
			}
			conn.commit();
			conn.setAutoCommit(true);

		} catch (Exception ex) {
			log.error("the insert into logtag exception:" + ex.getMessage());
			log.error("the insert into logtag sql is:" + sqlTag);
		} finally {
			try {
				pstmt.close();
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

	}

	public void updateLog(LogInfo li, String logNo) {
		String logCategory = li.getLogCategory();
		String isQuote = li.getIsQuote();
		String isShow = li.getIsShow();
		String logAuthor = li.getLogAuthor();
		String logContent = li.getLogContent();
		String logIntro = li.getLogIntro();
		String logPostTime = li.getLogPostTime();
		String logQuoteId = li.getLogQuoteId();
		String logQuotenums = li.getLogQuotenums();
		String logTitle = li.getLogTitle();
		String logtypeId = li.getLogtypeId();
		String replynum = li.getReplynum();
		String viewnums = li.getViewnums();
		String logTag = li.getLogTag();
		String sql = "update  log_info set logtype_id=" + logtypeId
				+ ",log_title='" + logTitle + "',log_intro='" + logIntro
				+ "',log_author='";
		sql += logAuthor + "',isQuote=" + isQuote + ",log_content='"
				+ logContent + "',log_postTime='" + logPostTime + "',viewnums="
				+ viewnums + ",log_quotenums=";
		sql += logQuotenums + ",log_modifytime=now(),isShow=" + isShow
				+ ",replynum=" + replynum + ",log_quote_id='" + logQuoteId
				+ "',log_category='";
		sql += logCategory + "',log_tag='" + logTag + "' where log_no='"
				+ logNo + "'";

		try {

			conn = DBFactory.getConnection();

			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
		} catch (Exception e) {
			log.error("the update loginfo exception:" + e.getMessage());
			log.error("the update loginfo sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

	}

	public void del(String logNo) {

		String sql = "delete from log_info where log_no='" + logNo + "'";
		try {

			conn = DBFactory.getConnection();

			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
		} catch (Exception e) {
			log.error("the delete loginfo exception:" + e.getMessage());
			log.error("the delete loginfo sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

	}

	// ��ɾ��
	public void del(String[] logNos) {

		String delSQL = "delete from log_info where log_no=?";
		try {
			conn = DBFactory.getConnection();
			for (int i = 0; i < logNos.length; i++) {
				stmt = conn.createStatement();
				pstmt = conn.prepareStatement(delSQL);
				pstmt.setString(1, logNos[i]);
				pstmt.executeUpdate();
				pstmt.close();
			}
		} catch (Exception e) {
			log.error("the delete loginfo exception:" + e.getMessage());
			log.error("the delete loginfo sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

	}

	public void updateLogByQuote(String logNo) {
		String sql = "update  log_info set log_quote_id='" + logNo
				+ "'  where log_no='" + logNo + "'";

		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
		} catch (Exception e) {
			log.error("the update loginfo exception:" + e.getMessage());
			log.error("the update loginfo sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

	}

	public void updateLogByDeleteQuote(String logNo) {
		String sql = "update  log_info set log_quote_id='1'  where log_no='"
				+ logNo + "'";

		try {
			conn = DBFactory.getConnection();
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
		} catch (Exception e) {
			log.error("the update loginfo exception:" + e.getMessage());
			log.error("the update loginfo sql is: " + sql);
		} finally {
			try {
				stmt.close();
				conn.close();
			} catch (SQLException sqle) {
				log.error(sqle.toString());
			}
		}

	}

}
